package cn.bincker.mycaptcha;

import cn.bincker.mycaptcha.basic.CaptchaGuard;
import cn.bincker.mycaptcha.basic.VerifyFailHandler;
import cn.bincker.mycaptcha.basic.filter.MyCaptchaFilter;
import cn.bincker.mycaptcha.basic.impl.DefaultVerifyFailHandler;
import cn.bincker.mycaptcha.letter.LetterCaptchaConfiguration;
import cn.bincker.mycaptcha.slider.SliderCaptchaConfiguration;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.List;

@Configuration
@AutoConfigureAfter(JacksonAutoConfiguration.class)
@ConditionalOnBean(ObjectMapper.class)
@Import({
        SliderCaptchaConfiguration.class,
        LetterCaptchaConfiguration.class
})
public class MyCaptchaAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public VerifyFailHandler verifyFailHandler(ObjectMapper objectMapper){
        return new DefaultVerifyFailHandler(objectMapper);
    }

    @Bean
    public MyCaptchaFilter filter(List<CaptchaGuard> guards, VerifyFailHandler verifyFailHandler){
        return new MyCaptchaFilter(guards, verifyFailHandler);
    }
}
